﻿#include "FlameQueen.h"
#include "../../../Shared/Functions/Functions.h"
#include "../../MirEnvir/Envir.h"
#include "../../../Shared/Data/Stat.h"
#include "../../../Shared/ServerPackets.h"
#include "../MapObject.h"
#include "../DelayedAction.h"
#include "../../../Shared/Enums.h"

using namespace Server::MirDatabase;
using namespace Server::MirEnvir;
namespace S = ServerPackets;

namespace Server::MirObjects::Monsters
{

    FlameQueen::FlameQueen(MonsterInfo *info) : MonsterObject(info)
    {
    }

    bool FlameQueen::InAttackRange()
    {
        return getCurrentMap() == getTarget()->getCurrentMap() && Functions::InRange(getCurrentLocation(), getTarget()->getCurrentLocation(), AttackRange);
    }

    void FlameQueen::Attack()
    {
        if (!getTarget()->IsAttackTarget(this))
        {
            setTarget(nullptr);
            return;
        }

        setDirection(Functions::DirectionFromPoint(getCurrentLocation(), getTarget()->getCurrentLocation()));

        if ((HP * 100 / Stats[Stat::HP]) < 20 && MassAttackTime < getEnvir()->getTime())
        {
            ShockTime = 0;
            ActionTime = getEnvir()->getTime() + 500;
            AttackTime = getEnvir()->getTime() + (AttackSpeed);

            S::ObjectRangeAttack *tempVar = new S::ObjectRangeAttack();
            tempVar->ObjectID = ObjectID;
            tempVar->Direction = getDirection();
            tempVar->Location = getCurrentLocation();
            tempVar->Type = 0;
            Broadcast(tempVar);

            std::vector<MapObject*> targets = FindAllTargets(7, getCurrentLocation(), false);

            if (targets.empty())
            {
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
                return;
            }

            int damage = GetAttackPower(Stats[Stat::MinDC], Stats[Stat::MaxDC]);

            for (int i = 0; i < targets.size(); i++)
            {
                int delay = Functions::MaxDistance(getCurrentLocation(), targets[i]->getCurrentLocation()) * 50 + 750; //50 MS per Step

                DelayedAction *action = new DelayedAction(DelayedType::RangeDamage, getEnvir()->getTime() + delay, {getTarget(), damage, DefenceType::ACAgility});
                ActionList.push_back(action);

//C# TO C++ CONVERTER TODO TASK: A 'delete action' statement was not added since action was passed to a method or constructor. Handle memory management manually.
            }

            MassAttackTime = getEnvir()->getTime() + 2000 + (getEnvir()->Random->Next(5) * 1000);
            ActionTime = getEnvir()->getTime() + 800;
            AttackTime = getEnvir()->getTime() + (AttackSpeed);

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
            return;
        }

        if (!Functions::InRange(getCurrentLocation(), getTarget()->getCurrentLocation(), 1) || getEnvir()->Random->Next(3) == 0)
        {
            S::ObjectAttack *tempVar2 = new S::ObjectAttack();
            tempVar2->ObjectID = ObjectID;
            tempVar2->Direction = getDirection();
            tempVar2->Location = getCurrentLocation();
            tempVar2->Type = 1;
            Broadcast(tempVar2);

            int damage = GetAttackPower(Stats[Stat::MinDC], Stats[Stat::MaxDC]);

            DelayedAction *action = new DelayedAction(DelayedType::Damage, getEnvir()->getTime() + 300, {getTarget(), damage, DefenceType::ACAgility});
            ActionList.push_back(action);

//C# TO C++ CONVERTER TODO TASK: A 'delete action' statement was not added since action was passed to a method or constructor. Handle memory management manually.
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar2' statement was not added since tempVar2 was passed to a method or constructor. Handle memory management manually.
        }
        else
        {
            S::ObjectAttack *tempVar3 = new S::ObjectAttack();
            tempVar3->ObjectID = ObjectID;
            tempVar3->Direction = getDirection();
            tempVar3->Location = getCurrentLocation();
            tempVar3->Type = 0;
            Broadcast(tempVar3);

            int damage = GetAttackPower(Stats[Stat::MinDC], Stats[Stat::MaxDC]);

            DelayedAction *action = new DelayedAction(DelayedType::RangeDamage, getEnvir()->getTime() + 300, {getTarget(), damage, DefenceType::ACAgility});
            ActionList.push_back(action);

//C# TO C++ CONVERTER TODO TASK: A 'delete action' statement was not added since action was passed to a method or constructor. Handle memory management manually.
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar3' statement was not added since tempVar3 was passed to a method or constructor. Handle memory management manually.
        }

        ShockTime = 0;
        ActionTime = getEnvir()->getTime() + 500;
        AttackTime = getEnvir()->getTime() + (AttackSpeed);
    }
}
